package com.geeklan.smart.ui.banner;

import android.content.Context;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.geeklan.smart.data.model.BannerResponse;
import com.geeklan.smart.databinding.LayoutBannerBinding;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.List;


public class BannerView extends RelativeLayout {

    private BannerAdapter bannerAdapter;
    private LayoutBannerBinding mBinding;
    private Context mContext;
    private int currentPage = 0; // 当前页面
    private List<BannerResponse.Banner> bannerList;
    public BannerView(@NonNull Context context) {
        super(context);
        mContext = context;
        mBinding = LayoutBannerBinding.inflate(LayoutInflater.from(getContext()), this, false);
    }

    public BannerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mBinding = LayoutBannerBinding.inflate(LayoutInflater.from(getContext()), this, false);
    }

    public BannerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        mBinding = LayoutBannerBinding.inflate(LayoutInflater.from(getContext()), this, false);
    }

    public void setBannerData(List<BannerResponse.Banner> bannerList) {
        this.bannerList = bannerList;
        removeAllViews();
        // Create the Banner Adapter and set it to ViewPager2
        bannerAdapter = new BannerAdapter(mContext,bannerList);
        mBinding.viewPager.setAdapter(bannerAdapter);
        mBinding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mBinding.indicator.setCurrentPosition(position);
            }
        });
        // 设置页面数量
        mBinding.indicator.setTotalCount(bannerList.size());

        // 设置圆形指示器的大小
        mBinding.indicator.setCircleSize(5);  // 设置圆形指示器半径

        // 设置矩形指示器的宽度
        mBinding.indicator.setRectangleWidth(18);  // 设置矩形指示器宽度

        // 设置指示器之间的间距
        mBinding.indicator.setIndicatorSpacing(10);  // 设置指示器间距
        mBinding.indicator.setCurrentPosition(0);

        handler.sendEmptyMessageDelayed(0,5000);
        addView(mBinding.getRoot());
    }

    Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            currentPage = (currentPage + 1) % bannerList.size(); // 循环
            mBinding.viewPager.setCurrentItem(currentPage, true); // 设置页面并平滑过渡
            handler.sendEmptyMessageDelayed(0,5000);
            return false;
        }
    });
}

